﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AjaxControlToolkit;

[assembly: WebResource("EnhancedWebControls.Resources.CalendarIcon.png", "image/png")] 
namespace EnhancedWebControls
{
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:EnhancedCalendar runat=server></{0}:EnhancedCalendar>"),
    DefaultEvent("TextChanged")]
    public class EnhancedCalendar : EnhancedTextBox
    {
        #region Child Controls
        protected CalendarExtender _calendarPicker;
        protected LinkButton _calendarButton;
        private string _defaultCalendarButtonImageUrl;
        #endregion

        #region Exposed Child Control Properties
        public FirstDayOfWeek CalendarFirstDayOfWeek
        {
            get 
            {
                EnsureChildControls();
                return _calendarPicker.FirstDayOfWeek;
            }
            set 
            {
                EnsureChildControls();
                _calendarPicker.FirstDayOfWeek = value;
            }
        }
        public CalendarPosition CalendarPopupPosition
        {
            get 
            {
                EnsureChildControls();
                return _calendarPicker.PopupPosition;
            }
            set 
            {
                EnsureChildControls();
                _calendarPicker.PopupPosition = value;
            }
        }
        public string CalendarFormatOverride
        {
            get 
            {
                EnsureChildControls();
                return _calendarPicker.Format;
            }
            set 
            {
                EnsureChildControls();
                _calendarPicker.Format = value;
            }
        }
        public Unit CalendarButtonWidth { get; set; }
        public string CalendarButtonImageUrl { get; set; }
        #endregion

        #region Overriden Methods
        public EnhancedCalendar()
            : base()
        {
            CalendarButtonWidth = new Unit(31, UnitType.Pixel);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();
            RenderDesignTimeStyles(writer);
            RenderContainerBegin(writer, false);
            RenderLeftSide(writer);
            RenderRightContainerBegin(writer);
            if (LabelMode)
                RenderRightLabel(writer);
            else
            {
                writer.AddStyleAttribute("float", "left");
                writer.AddStyleAttribute("clear", "none");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                RenderRightTextBox(writer, (int)CalendarButtonWidth.Value);
                writer.RenderEndTag();
                writer.AddStyleAttribute("float", "left");
                writer.AddStyleAttribute("clear", "none");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                _calendarButton.CssClass = "EWC__CalendarIcon";
                _calendarButton.Style["background-image"] = "url(" + CalendarButtonImageUrl + ")";
                _calendarButton.Style["background-repeat"] = "no-repeat";
                _calendarButton.RenderControl(writer);
                writer.RenderEndTag();
                _calendarPicker.RenderControl(writer);
            }
            RenderRightContainerEnd(writer);
            RenderContainerEnd(writer);
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            base.CreateChildControls();
            _calendarButton = new LinkButton();
            _calendarButton.ID = "CalendarButton";
            Controls.Add(_calendarButton);
            _calendarPicker = new CalendarExtender();
            _calendarPicker.ID = "CalendarPicker";
            _calendarPicker.TargetControlID = _rightTextBox.UniqueID;
            _calendarPicker.PopupButtonID = _calendarButton.UniqueID;
            Controls.Add(_calendarPicker);
            _rightTextBox.AutoCompleteType = AutoCompleteType.Disabled;
            //RightTextBox.Attributes.Add("autocomplete", "off");
            _leftLabel.AssociatedControlID = _rightTextBox.ID;
        }
        protected override void OnPreRender(System.EventArgs e)
        {
            PhoneMode = false;
            if (AutoPostBackMode == AutoPostBackModes.OnKeyUp)
                throw new NotSupportedException("The Calendar Control Doesn't support the OnKeyUp AutoPostBackMode");
            base.OnPreRender(e);
            if (string.IsNullOrEmpty(CalendarButtonImageUrl))
                CalendarButtonImageUrl = _defaultCalendarButtonImageUrl;
            if (!LabelMode && Visible && Enabled)
                SetCalendarPickerClientScripts();
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _defaultCalendarButtonImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "EnhancedWebControls.Resources.CalendarIcon.png");
        }
        #endregion

        #region Helper Methods
        protected void SetCalendarPickerClientScripts()
        {
           _calendarPicker.OnClientHidden = "function () {" + GetOnBlurClientScript(RightTextBox.ClientID) + ";}";
           _calendarPicker.OnClientShown = "function () {" + GetOnFocusClientScript(RightTextBox.ClientID) + ";}";
        }
        #endregion
    }
}
